package com.lc.luzhengtang.fragment;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.CouponListAlreadyUseAdapter;
import com.lc.luzhengtang.adapter.CouponListNotUseAdapter;
import com.lc.luzhengtang.adapter.CouponListOutDateAdapter;
import com.lc.luzhengtang.bean.CouponAlreadyuseBean;
import com.lc.luzhengtang.bean.CouponBean;
import com.lc.luzhengtang.bean.CouponNotuseBean;
import com.lc.luzhengtang.bean.CouponOutDataBean;
import com.lc.luzhengtang.conn.ConnCoupon;
import com.lc.luzhengtang.divider.SimpleDividerItemDecoration;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.lc.luzhengtang.R.id.card_title_tv;

/**
 * Created by Administrator on 2017/5/5.
 */

public class CouponFragment extends BaseFragment3 implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(card_title_tv)
    TextView cardTitleTv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    Unbinder unbinder;
    private String mTitle;

    //    private CouponListAdapter newsAdapter;
    private int currentPage = 1;

    List<CouponNotuseBean> list_notuse = new ArrayList<>();
    CouponListNotUseAdapter couponListNotUseAdapter;

    List<CouponAlreadyuseBean> couponAlreadyuseBeanList = new ArrayList<>();
    CouponListAlreadyUseAdapter couponListAlreadyUseAdapter;
    //已过期
    CouponListOutDateAdapter couponListAdapter;
    List<CouponOutDataBean> couponoutData = new ArrayList<>();

    public static CouponFragment getInstance(String title) {
        CouponFragment sf = new CouponFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_coupon, null);

        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        card_title_tv.setText(mTitle);
        Toast.makeText(getActivity(), mTitle, Toast.LENGTH_SHORT).show();
        unbinder = ButterKnife.bind(this, v);
        initData();
        return v;
    }

    @Override
    protected void initData() {
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

      /*  newsAdapter = new CouponListAdapter(null);
        newsAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        newsAdapter.isFirstOnly(false);*/
/*
        if (mTitle.equals("已过期")){
            couponListAdapter=new CouponListOutDateAdapter(null);
            couponListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
            couponListAdapter.isFirstOnly(false);
            Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider2);
            recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 40));

            recyclerView.setAdapter(couponListAdapter);
        }else if (mTitle.equals("已使用")){
            couponListAlreadyUseAdapter=new CouponListAlreadyUseAdapter(null);
            couponListAlreadyUseAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
            couponListAlreadyUseAdapter.isFirstOnly(false);
            Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider2);
            recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 40));
            recyclerView.setAdapter(couponListAlreadyUseAdapter);
        }else {
            couponListNotUseAdapter=new CouponListNotUseAdapter(null);
            couponListNotUseAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
            couponListNotUseAdapter.isFirstOnly(false);
            Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider2);
            recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 40));
            recyclerView.setAdapter(couponListNotUseAdapter);
        }*/
        couponListNotUseAdapter = new CouponListNotUseAdapter(null);
        couponListNotUseAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        couponListNotUseAdapter.isFirstOnly(false);
        Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider2);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 40));
        recyclerView.setAdapter(couponListNotUseAdapter);
        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);


        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    public void getData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        new ConnCoupon(BaseApplication.basePreferences.readUid(), new AsyCallBack<CouponBean>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CouponBean couponBean) throws Exception {
                super.onSuccess(toast, type, o, couponBean);
                List<CouponBean.DataBean> results = couponBean.getData();
                if (results != null) {
                    list_notuse.clear();
                    couponAlreadyuseBeanList.clear();
                    couponoutData.clear();

                    currentPage = 1;

                }
                /*else if (list_notuse!=null){
                    couponListNotUseAdapter.setNewData(list_notuse);
                }else if (couponoutData !=null){
                    couponListAdapter.setNewData(couponoutData);
                }*/

                for (CouponBean.DataBean people : results) {
                    if (people.getStatus() == 1) {
                        CouponNotuseBean couponNotuseBean = new CouponNotuseBean();
                        couponNotuseBean.setId(people.getId());
                        couponNotuseBean.setBigmoney(people.getBigmoney());
                        couponNotuseBean.setEnd_time(people.getEnd_time());
                        couponNotuseBean.setStart_time(people.getStart_time());
                        couponNotuseBean.setSmallmoney(people.getSmallmoney());
                        list_notuse.add(couponNotuseBean);
                    }

                }
                //未使用
                couponListNotUseAdapter = new CouponListNotUseAdapter(list_notuse);

                for (CouponBean.DataBean people : results) {
                    if (people.getStatus() == 2) {
                        CouponAlreadyuseBean couponNotuseBean = new CouponAlreadyuseBean();
                        couponNotuseBean.setId(people.getId());
                        couponNotuseBean.setBigmoney(people.getBigmoney());
                        couponNotuseBean.setEnd_time(people.getEnd_time());
                        couponNotuseBean.setStart_time(people.getStart_time());
                        couponNotuseBean.setSmallmoney(people.getSmallmoney());
                        couponAlreadyuseBeanList.add(couponNotuseBean);
                    }

                }


                for (CouponBean.DataBean people : results) {

                    if (people.getStatus() == 3) {

                        CouponOutDataBean couponNotuseBean = new CouponOutDataBean();
                        couponNotuseBean.setId(people.getId());
                        couponNotuseBean.setBigmoney(people.getBigmoney());
                        couponNotuseBean.setEnd_time(people.getEnd_time());
                        couponNotuseBean.setStart_time(people.getStart_time());
                        couponNotuseBean.setSmallmoney(people.getSmallmoney());
                        couponoutData.add(couponNotuseBean);
                    }

                }
                couponListNotUseAdapter.setNewData(list_notuse);
                couponListAlreadyUseAdapter.setNewData(couponAlreadyuseBeanList);
                couponListAdapter.setNewData(couponoutData);

                //已使用
//                couponListAdapter = new CouponListOutDateAdapter(couponoutData);

                for (int i = 0; i < couponoutData.size(); i++) {
                    Log.d(couponoutData.get(i).getEnd_time(), "33333333333333");
                    Log.d(couponoutData.get(i).getStart_time(), "33333333333333");
                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                setRefreshing(false);
                dismissLoading();
            }
        }


        ).execute(getActivity(), false);
    }


    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }
}
